using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

[assembly: TagPrefix("Wicresoft.Busi.WebControls", "wsc")]

namespace Wicresoft.Busi.WebControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:TimePicker runat=server></{0}:TimePicker>")]
    public class TimePicker : WebControl
    {
        private DropDownList ddlHour; 
        private DropDownList ddlMinute; 

        private string ViewStateKeyPreFix
        {
            get
            {
                return "SubMenu" + this.ID + "_";
            }
        }

        private int Hour
        {
            get
            {
                if (ddlHour != null && ddlHour.SelectedValue != null && ddlHour.SelectedValue != string.Empty)
                {
                    string hourString = ddlHour.SelectedValue;
                    if (hourString.EndsWith(":"))
                        hourString = hourString.Substring(0, hourString.Length - 1);
                    return int.Parse(hourString);
                }
                return 0;
            }
            set
            {
                ddlHour.SelectedValue = string.Format("{0}:", GetTimeString(value));
            }
        }

        private int Minute
        {
            get
            {
                if (ddlMinute != null && ddlMinute.SelectedValue != null && ddlMinute.SelectedValue != string.Empty)
                {
                    string minuteString = ddlMinute.SelectedValue;
                    return int.Parse(minuteString);
                }
                return 0;
            }
            set
            {
                int minute = value / 5 * 5;
                ddlMinute.SelectedValue = GetTimeString(minute);
            }
        }

        [Browsable(false)]
        public DateTime Time
        {
            get
            {
                return new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, Hour, Minute, 0);
            }
            set
            {
                Hour = value.Hour;
                Minute = value.Minute;
            }
        }

        private string HourID
        {
            get
            {
                return string.Format("{0}_ddlHour", this.ID);
            }
        }

        private string MinuteID
        {
            get
            {
                return string.Format("{0}_ddlMinute", this.ID);
            }
        }

        public override Unit Width
        {
            get
            {
                return base.Width;
            }
            set
            {
                base.Width = value;
            }
        }

        public string DropDownCSS
        {
            get
            {
                if (ViewState[ViewStateKeyPreFix + "_DropDownCSS"] == null)
                    ViewState[ViewStateKeyPreFix + "_DropDownCSS"] = "InputDropDown";
                return ViewState[ViewStateKeyPreFix + "_DropDownCSS"].ToString();
            }
            set
            {
                ViewState[ViewStateKeyPreFix + "_DropDownCSS"] = value;
            }
        }

        private string GetTimeString(int timeValue)
        {
            if (timeValue < 10)
                return string.Format("0{0}", timeValue);
            return timeValue.ToString();
        }

        protected override void  OnInit(EventArgs e)
        {
            ddlHour = new DropDownList();
            ddlMinute = new DropDownList();
            ddlHour.ID = this.HourID;
             
            //ddlHour.EnableViewState = true;

            ddlMinute.ID = this.MinuteID;
            //ddlMinute.EnableViewState = true;

            for (int i = 0; i < 24; i++)
            {
                ListItem li = new ListItem(string.Format("{0}:", GetTimeString(i)));
                ddlHour.Items.Add(li);
            }
            if (Page.Request[this.HourID] != null)
            {
                try
                {
                    ddlHour.SelectedValue = Page.Request[this.HourID];
                }
                catch
                {
                }
            }

            for (int i = 0; i < 60; i += 5)
            {
                ListItem li = new ListItem(string.Format("{0}", GetTimeString(i)));
                ddlMinute.Items.Add(li);
            }
            if (Page.Request[this.MinuteID] != null)
            {
                try
                {
                    ddlMinute.SelectedValue = Page.Request[this.MinuteID];
                }
                catch
                {
                }
            }
 	        base.OnInit(e);
        }
        
       


        protected override void CreateChildControls()
        {
            Unit width = new Unit(100, UnitType.Pixel);
            if (this.Width != null && this.Width.Value > 0)
                width = this.Width;
            ddlHour.Width = new Unit((width.Value - 4) / 2, width.Type);
            ddlHour.CssClass = this.DropDownCSS;
            ddlHour.Enabled = this.Enabled;

            ddlMinute.Width = new Unit((width.Value - 4) / 2, width.Type);
            ddlMinute.CssClass = this.DropDownCSS;
            ddlMinute.Enabled = this.Enabled;

            HtmlGenericControl span1 = new HtmlGenericControl("span");
            span1.InnerText = " ";

            this.Controls.Add(ddlHour);
            this.Controls.Add(span1);
            this.Controls.Add(ddlMinute);

            base.CreateChildControls();
        } 

    }
}
